package net.cloudcodex.client.character.presenter;

import java.util.ArrayList;
import java.util.List;

import net.cloudcodex.client.CloudCodexEventBus;
import net.cloudcodex.client.campaign.view.CampaignsView.Message;
import net.cloudcodex.client.character.view.CharactersInCampaignView;
import net.cloudcodex.client.character.view.CharactersInCampaignViewImpl;
import net.cloudcodex.shared.dto.campaign.character.CharacterHeaderDTO;
import net.cloudcodex.shared.dto.home.CampaignSummaryDTO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

/**
 * 
 * 
 * @author ADaroussin
 */
@Presenter(view = CharactersInCampaignViewImpl.class, multiple = true)
public class CharactersInCampaignPresenter extends
		BasePresenter<CharactersInCampaignView, CloudCodexEventBus> {
	private final Message message;
	private List<CharacterHeaderDTO> characterHeaderDTOList;
	private CampaignSummaryDTO campaignSummaryDTO;

	@Inject
	public CharactersInCampaignPresenter(Message pMessage) {
		this.message = pMessage;
	}

	@Override
	public void bind() {
		this.view.getAddNPCButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				eventBus.createNewNPC(campaignSummaryDTO);
			}
		});
		this.view.getAddPCButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				eventBus.inviteNewPlayer(campaignSummaryDTO);
			}
		});


	}

	public void addCharacterHeaderList(
			List<CharacterHeaderDTO> characterHeaderDTOList) {
		this.characterHeaderDTOList = characterHeaderDTOList;
	}

	public void initView() {
		if (characterHeaderDTOList != null && !characterHeaderDTOList.isEmpty()) {
			for (CharacterHeaderDTO characterHeaderDTO : characterHeaderDTOList) {
				initCharacterInCampaignView(characterHeaderDTO);
				
			}
		}
	}

	/**
	 * @param characterHeaderDTO
	 */
	public void initCharacterInCampaignView(
			CharacterHeaderDTO characterHeaderDTO) {
		CharacterInCampaignPresenter characterInCampaignPresenter = eventBus
				.addHandler(CharacterInCampaignPresenter.class);
		characterInCampaignPresenter.addCharacterHeader(campaignSummaryDTO.getId(),characterHeaderDTO);
		characterInCampaignPresenter.initView();
		this.view.addCharacterView(characterInCampaignPresenter.getView());
	}

	public void addCampaignSummaryDTO(CampaignSummaryDTO campaignSummaryDTO) {
		this.campaignSummaryDTO = campaignSummaryDTO ;
	}

	public void addNewNPCInCampaign(CharacterHeaderDTO characterHeaderDTO) {
		if(characterHeaderDTOList == null){
			characterHeaderDTOList = new ArrayList<CharacterHeaderDTO>();
		}
		this.characterHeaderDTOList.add(characterHeaderDTO);
		initCharacterInCampaignView(characterHeaderDTO);
		
	}
	

}
